<?php


namespace app\api\controller\v1;



use app\api\service\BaseToken;
use app\api\validate\IDIsInt;
use app\api\validate\OrdersValidate;
use app\api\validate\SummaryValidate;
use app\lib\exception\OrderException;

class Orders extends BaseApi
{
    // 前置验证
    protected $beforeActionList = [
        'checkScope' => ['only' => 'placeOrder']
    ];

    // 历史订单
    public function getSummaryByUser($page=1, $size=15)
    {
        (new SummaryValidate())->goCheck();
        $uid = BaseToken::getCurrentUid();
        $pageOrders = \app\api\model\Orders::getSummaryByUser($uid, $page, $size);
        if ($pageOrders->isEmpty()) {
            return [
                'data' => [],
                'current_page' => $pageOrders->getCurrentPage()
            ];
        }
        $data = $pageOrders->toArray();
        return [
            'data' => $data,
            'current_page' => $pageOrders->getCurrentPage()
        ];
    }

    // 订单详情
    public function getOrdersDetail($id)
    {
        (new IDIsInt())->goCheck();
        $ordersDetail = \app\api\model\Orders::get($id);
        if (!$ordersDetail) {
            throw new OrderException();
        }
        return $ordersDetail->visible(['id', 'snap_items', 'orders_state', 'orders_price', 'orders_num', 'orders_code', 'snap_img', 'snap_name']);
    }

    public function placeOrder()
    {
        (new OrdersValidate())->goCheck();
        // 获取客户端传递的订单商品数组参数
        $dish = input('post.products/a');
        $uid = BaseToken::getCurrentUid();
        $orders = new \app\api\service\Orders();
        $status = $orders->place($uid, $dish);
        return $status;
    }
}